package com.hph.health.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ErrorCode;
import com.hph.health.common.ResultUtils;
import com.hph.health.exception.BusinessException;
import com.hph.health.exception.ThrowUtils;
import com.hph.health.mapper.FavoriteMapper;
import com.hph.health.model.dto.favorite.FavoriteRequest;
import com.hph.health.model.dto.user.DeleteRequest;
import com.hph.health.model.entity.Favorite;
import com.hph.health.service.FavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * 收藏接口
 *
 * @author hanph
 * @from haut
 */
@RestController
@RequestMapping("/favorite")
@Slf4j
public class FavoriteController {
    @Resource
    private FavoriteService favoriteService;
    @Resource
    private FavoriteMapper favoriteMapper;
    /**
     * 获取收藏列表
     *
     * @param favoriteRequest
     * @return
     */
    @PostMapping("/list")
    public BaseResponse<List<Favorite>> listProductByPage(@RequestBody FavoriteRequest favoriteRequest) {
        String userId = favoriteRequest.getUserId();
        String productId = favoriteRequest.getProductId();
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userId),"user_id", userId);
        queryWrapper.eq(StringUtils.isNotBlank(productId),"product_id", productId);
        List<Favorite> list = favoriteService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     *  新增收藏
     *
     * @param favoriteRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addProduct(@RequestBody FavoriteRequest favoriteRequest) {
        if (favoriteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Favorite favorite = new Favorite();
        BeanUtils.copyProperties(favoriteRequest, favorite);
        // 不能重复收藏
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", favorite.getUserId());
        queryWrapper.eq("product_id", favorite.getProductId());
        long count = favoriteMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "此商品您已收藏");
        }
        boolean result = favoriteService.save(favorite);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(favorite.getId());
    }

    /**
     * 删除收藏
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProduct(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(favoriteService.removeById(deleteRequest.getId()));
    }

}
